package com.xinghai.web.controller.dingtalk;

import com.xinghai.common.annotation.Log;
import com.xinghai.common.core.controller.BaseController;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.core.page.TableDataInfo;
import com.xinghai.common.enums.BusinessType;
import com.xinghai.common.exception.ServiceException;
import com.xinghai.common.utils.poi.ExcelUtil;
import com.xinghai.system.domain.XhUseVehicleInfo;
import com.xinghai.system.domain.XhVehicleOrderInfo;
import com.xinghai.system.domain.dto.*;
import com.xinghai.system.domain.vo.XhVehicleInfoQueryVo;
import com.xinghai.system.service.IXhVehicleOrderInfoService;
import com.xinghai.system.util.BeanMapperUtils;
import com.xinghai.web.controller.dingtalk.vo.XhUseVehicleInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用车订单Controller
 * 
 * @author xinghai
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/order/info")
public class XhVehicleOrderInfoController extends BaseController
{
    @Autowired
    private IXhVehicleOrderInfoService xhVehicleOrderInfoService;

    /**
     * 查询用车订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(XhVehicleOrderInfo xhVehicleOrderInfo)
    {
        startPage();
        List<XhVehicleOrderInfoDTO> list = xhVehicleOrderInfoService.list(xhVehicleOrderInfo);
        return getDataTable(list);
    }

    /**
     * 查询失效订单列表 expires
     */
    @GetMapping("/expiresList")
    public TableDataInfo expiresList(XhVehicleOrderInfo xhVehicleOrderInfo)
    {
        startPage();
        List<XhVehicleOrderInfoDTO> list = xhVehicleOrderInfoService.expiresList(xhVehicleOrderInfo);
        return getDataTable(list);
    }

    /**
     * 待接单导出
     */
    @Log(title = "待接单导出", businessType = BusinessType.EXPORT)
    @PostMapping("/wait/export")
    public void waitExport(HttpServletResponse response, XhVehicleOrderInfo xhVehicleOrderInfo)
    {
        List<XhWaitOrderInfoDTO> list = xhVehicleOrderInfoService.list(xhVehicleOrderInfo).stream().map(m->{
            XhWaitOrderInfoDTO expiresOrderInfoDTO = new XhWaitOrderInfoDTO();
            BeanUtils.copyProperties(m,expiresOrderInfoDTO);
            return expiresOrderInfoDTO;
        }).collect(Collectors.toList());
        ExcelUtil<XhWaitOrderInfoDTO> util = new ExcelUtil<XhWaitOrderInfoDTO>(XhWaitOrderInfoDTO.class);
        util.exportExcel(response, list, "待接承运单");
    }

    /**
     * 待审批承运单导出
     */
    @Log(title = "待审批承运单导出", businessType = BusinessType.EXPORT)
    @PostMapping("/apply/export")
    public void applyExport(HttpServletResponse response, XhVehicleOrderInfo xhVehicleOrderInfo)
    {
        List<XhApplyOrderInfoDTO> list = xhVehicleOrderInfoService.list(xhVehicleOrderInfo).stream().map(m->{
            XhApplyOrderInfoDTO expiresOrderInfoDTO = new XhApplyOrderInfoDTO();
            BeanUtils.copyProperties(m,expiresOrderInfoDTO);
            return expiresOrderInfoDTO;
        }).collect(Collectors.toList());
        ExcelUtil<XhApplyOrderInfoDTO> util = new ExcelUtil<XhApplyOrderInfoDTO>(XhApplyOrderInfoDTO.class);
        util.exportExcel(response, list, "承运单信息");
    }

    /**
     * 导出用车订单列表
     */
    @Log(title = "用车订单", businessType = BusinessType.EXPORT)
    @PostMapping("/expires/export")
    public void expiresExport(HttpServletResponse response, XhVehicleOrderInfo xhVehicleOrderInfo)
    {
        List<XhExpiresOrderInfoDTO> list = xhVehicleOrderInfoService.expiresList(xhVehicleOrderInfo).stream().map(m->{
            XhExpiresOrderInfoDTO expiresOrderInfoDTO = new XhExpiresOrderInfoDTO();
            BeanUtils.copyProperties(m,expiresOrderInfoDTO);
            return expiresOrderInfoDTO;
        }).collect(Collectors.toList());
        ExcelUtil<XhExpiresOrderInfoDTO> util = new ExcelUtil<XhExpiresOrderInfoDTO>(XhExpiresOrderInfoDTO.class);
        util.exportExcel(response, list, "失效承运单");
    }

    /**
     * 导出用车订单列表
     */
    @Log(title = "用车订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XhVehicleOrderInfo xhVehicleOrderInfo)
    {
        List<XhVehicleOrderInfoDTO> list = xhVehicleOrderInfoService.list(xhVehicleOrderInfo);
        ExcelUtil<XhVehicleOrderInfoDTO> util = new ExcelUtil<XhVehicleOrderInfoDTO>(XhVehicleOrderInfoDTO.class);
        util.exportExcel(response, list, "用车订单数据");
    }

    /**
     * 获取用车订单详细信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return success(xhVehicleOrderInfoService.getInfo(id));
    }

    /**
     * 新增用车订单
     */
    @Log(title = "用车订单", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody XhVehicleOrderInfo xhVehicleOrderInfo)
    {
        return toAjax(xhVehicleOrderInfoService.insert(xhVehicleOrderInfo));
    }

    /**
     * 修改用车订单
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "用车订单", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody XhVehicleOrderInfo xhVehicleOrderInfo)
    {
        return toAjax(xhVehicleOrderInfoService.update(xhVehicleOrderInfo));
    }

    /**
     * 删除用车订单
     */
    @Log(title = "用车订单", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult delete(Long id)
    {
        return toAjax(xhVehicleOrderInfoService.delete(id));
    }


    /**
     * 批量删除用车订单
     */
    @Log(title = "用车订单", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(Long[] ids)
    {
        return toAjax(xhVehicleOrderInfoService.deleteBatch(ids));
    }

    /**
     * 导入用车订单模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<XhVehicleOrderInfo> util = new ExcelUtil<XhVehicleOrderInfo>(XhVehicleOrderInfo.class);
        util.importTemplateExcel(response, "用车订单数据模板");
    }


    /**
     * 导入用车订单
     */
    @Log(title = "用车订单管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        ExcelUtil<XhVehicleOrderInfo> util = new ExcelUtil<XhVehicleOrderInfo>(XhVehicleOrderInfo.class);
        List<XhVehicleOrderInfo> xhVehicleOrderInfoList = null;
        try {
            xhVehicleOrderInfoList = util.importExcel(file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException("导入数据失败");
        }
        return success(xhVehicleOrderInfoService.importData(xhVehicleOrderInfoList));
    }

    /**
     * 物流公司接单
     */
    @Log(title = "物流公司接单", businessType = BusinessType.UPDATE)
    @PostMapping("/accept/order")
    public AjaxResult acceptOrder(@RequestBody XhVehicleOrderInfo xhVehicleOrderInfo)
    {
        try {
            return toAjax(xhVehicleOrderInfoService.acceptOrder(xhVehicleOrderInfo));
        }catch (Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 物流公司-拒绝接单
     */
    @Log(title = "物流公司拒绝接单", businessType = BusinessType.UPDATE)
    @PostMapping("/refuse/order")
    public AjaxResult refuseOrder(@RequestBody XhVehicleOrderInfo xhVehicleOrderInfo)
    {
        try {
            return toAjax(xhVehicleOrderInfoService.refuseOrder(xhVehicleOrderInfo));
        }catch (Exception e){
            return error(e.getMessage());
        }
    }

    /**
     * 物流公司更换车辆信息
     */
    @Log(title = "物流公司更换车辆信息", businessType = BusinessType.UPDATE)
    @PostMapping("/change/order")
    public AjaxResult changeOrder(@RequestBody XhVehicleOrderInfo xhVehicleOrderInfo)
    {
        try {
            return toAjax(xhVehicleOrderInfoService.changeOrder(xhVehicleOrderInfo));
        }catch (Exception e){
            return error(e.getMessage());
        }
    }

    /**
     * 物流公司获取待更换车辆信息
     */
    @Log(title = "物流公司更换车辆信息", businessType = BusinessType.UPDATE)
    @GetMapping("/wait/change/info")
    public AjaxResult waitChangeInfo(XhVehicleOrderInfo xhVehicleOrderInfo)
    {
        try {
            return success(xhVehicleOrderInfoService.waitChangeInfo(xhVehicleOrderInfo));
        }catch (Exception e){
            return error(e.getMessage());
        }
    }

    /**
     * 根据状态查询用车订单列表
     */
    @GetMapping("/getListByStatus/{status}")
    public TableDataInfo getListByStatus(XhVehicleInfoQueryVo xhVehicleInfoQueryVo, @PathVariable("status") String status)
    {
        startPage();
        List<XhUseVehicleInfoDto> list = xhVehicleOrderInfoService.getListByStatus(xhVehicleInfoQueryVo,status);
        return getDataTable(list);
    }

    /**
     * 导出用车信息列表
     */
    @Log(title = "用车信息", businessType = BusinessType.EXPORT)
    @PostMapping("/listByStatus/export/{status}")
    public void listByStatusExport(HttpServletResponse response, XhVehicleInfoQueryVo xhVehicleInfoQueryVo,@PathVariable("status") String status) {
        String sheetName = "";
        //TODO 待修改 逻辑提取
        switch (status){
            case "2":
                sheetName = "进行中订单";
                break;
            case "3":
                sheetName = "已完成订单";
                break;
            case "4":
                sheetName = "已拒绝订单";
                break;
            case "5":
                sheetName = "已超时订单";
                break;
            default: sheetName = "";
                break;
        }
        List<XhUseVehicleInfoDto> list = xhVehicleOrderInfoService.getListByStatus(xhVehicleInfoQueryVo,status);
        //拒绝订单
        if (status.equals("4")) {
            ExcelUtil<XhUseVehicleInfoRejectDto> util = new ExcelUtil<XhUseVehicleInfoRejectDto>(XhUseVehicleInfoRejectDto.class);
            List<XhUseVehicleInfoRejectDto> rejectList = new ArrayList<>();
            for (XhUseVehicleInfoDto dto : list) {
                XhUseVehicleInfoRejectDto rejectDto = new XhUseVehicleInfoRejectDto();
                BeanUtils.copyProperties(dto, rejectDto);
                rejectList.add(rejectDto);
            }

            util.exportExcel(response, rejectList, sheetName);
        } else if (status.equals("5")) {
            //超时订单
            ExcelUtil<XhUseVehicleInfoTimeOutDto> util = new ExcelUtil<XhUseVehicleInfoTimeOutDto>(XhUseVehicleInfoTimeOutDto.class);
            List<XhUseVehicleInfoTimeOutDto> rejectList = new ArrayList<>();
            for (XhUseVehicleInfoDto dto : list) {
                XhUseVehicleInfoTimeOutDto timeOutDto = new XhUseVehicleInfoTimeOutDto();
                BeanUtils.copyProperties(dto, timeOutDto);
                rejectList.add(timeOutDto);
            }

            util.exportExcel(response, rejectList, sheetName);
        } else {
            ExcelUtil<XhUseVehicleInfoDto> util = new ExcelUtil<XhUseVehicleInfoDto>(XhUseVehicleInfoDto.class);
            util.exportExcel(response, list, sheetName);
        }
    }
}
